<template>
<div  class="navbar-right">
  <p>
    <span v-if="isAdminLogined" style="color: black;">
      管理员 {{admin.name}},你好！
      <a v-on:click="adminLogOut" href="#">退出</a>
    </span>
  </p>
</div>
</template>
<script>
import 'whatwg-fetch'
var user={
        email:"",
        session:""
      }
var admin={
        name:"",
        session:""
      }
window.user=user
window.admin=admin
export default  {
  name: 'session',
  data (){
    return {
      admin:admin,
      needAdminLogin:false,
      wasUserPath:true,
      isAdminLogined:false
    }
  },
  mounted:function () {
    this.refresh()
    this.inits()
  },
  watch: {
    user:function () {
      this.refresh()
    },
    isAdminLogined:function () {
      logger.log('admin status changed')
      this.refresh()
    },
    $route: function () {
      logger.log('route change')
      document.title="代码狗"
      this.refresh();
      this.inits()
    }
  },
  methods:{
      refresh:function () {
        var vm=this
        var path=''
        try {
          path=this.$route.path
        } catch (e) {
          path=location.pathname    
        }
        logger.log('path was:',path);
        var adminCookie=getCookie('admins')
        if(adminCookie!=null&&adminCookie!=""){
          var adminjson=JSON.parse(adminCookie)
          this.admin.name=adminjson.name
          this.admin.session=adminjson.session
          this.isAdminLogined=true
        }else{
          this.isAdminLogined=false
        }
        
        if (path.indexOf("/admin")==0){
          this.wasUserPath=false
          this.needAdminLogin=this.isAdminLogined||path.indexOf('/admin/login')==0?false:true
        }else{
          this.wasUserPath=true;
          this.needAdminLogin=false;
        }
        if(this.needAdminLogin){
          this.$router.push('/admin/login')
        }
      },
      inits:function () {
        var vm=this
        var path=''
        try {
          path=this.$route.path
        } catch (e) {
          path=location.pathname    
        }
        if(vm.admin.session!=""){
              fetch('/api/getAdminInfo', {
              method: 'POST',
              headers: {
                'Content-Type': 'application/json'
              },
              mode: 'cors',
              body: JSON.stringify({
                name: vm.admin.name,
                session: vm.admin.session,
                })
            }).then(function(response) {
                var json=response.json()
                return json
              }).then(function(json) {
                  if(json==null||json.code!=0){
                    delCookie('admins')
                    vm.admin.name=""
                    vm.admin.session=""
                    vm.isAdminLogined=false
                  }
              })
          }
      },
    adminLogOut:function(){
      var vm=this
      this.$confirm('此操作将退出登录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delCookie("admins")
          this.admin.name=''
          this.admin.session=''
          this.isAdminLogined=false
          this.$message({
            type: 'success',
            message: '成功退出!',
            onClose:function () {
              vm.$router.push('/admin/login')
            }
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

    }
  }
}
</script>
<style scoped>

</style>
